package com.base.common.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.operamasks.faces.annotation.DefineConverter;
import org.operamasks.faces.annotation.Format;
import org.operamasks.faces.component.layout.UIFacelet;

@DefineConverter(id = "dataTimeConverter")
public class TimeIntervalConverter implements Converter {

	@Format
	public String format(Object value) {
		FacesContext context = FacesContext.getCurrentInstance();
		return this.getAsString(context, new UIFacelet(), value);
	}

	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		return null;
	}

	public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
		long interval = 0;
		if (value instanceof Long) {
			interval = (Long) value;
		}
		long hour = interval / (1000 * 60 * 60);
		long minute = interval % (1000 * 60 * 60) / (1000 * 60);
		long second = interval % (1000 * 60) / (1000);
		StringBuffer buffer = new StringBuffer();
		buffer.append(hour).append("小时").append(minute).append("分").append(
				second).append("秒");
		return buffer.toString();
	}
}
